package com.action.wx.merchant;

import com.bean.Mb_account;
import com.bean.Member;
import com.bean.Merchant;
import com.common.BaseAction;
import com.common.SystemConfig;
import com.service.member.MbAccountService;
import com.service.member.MemberService;
import com.service.merchant.MerchantService;
import com.service.wxconfig.WxConfigService;
import com.util.Config;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/10/9.
 */
@Controller
@RequestMapping("wx/mt")
public class WxMerchantAction extends BaseAction {
    @Autowired
    private MemberService memberService;

    @Autowired
    private WxConfigService wxConfigService;

    @Autowired
    private MbAccountService mbAccountService;

    @Autowired
    private MerchantService merchantService;

    @RequestMapping("goPay")
    public ModelAndView goPay(String code,Integer state,ModelMap paraMap) {
        HttpSession session = this.getSession();
        Member member=null;
        Integer mtId = state;
        Integer mid=(Integer)session.getAttribute(SystemConfig.MemberIdSession);

        /*调试内容*/
        /*mid=9;
        member=memberService.selectByPrimaryKey(mid);
        session.setAttribute(SystemConfig.MemberIdSession,mid);
        session.setAttribute(SystemConfig.MemberSession,member);*/
        /*调试内容*/
        if(mid==null){
            if(StringUtils.isNotBlank(code)){
                member = wxConfigService.setMemberData(code, null);
                mid=member.getId();
            }else{
                String redirectUrl= Config.getString("login.center");
                return new ModelAndView("redirect:"+redirectUrl, paraMap);
            }
        }
        Merchant mt = merchantService.selectByPrimaryKey(mtId);

        Mb_account account=null;
        if(mid!=null){
            member=memberService.selectByPrimaryKey(mid);
            account = mbAccountService.selectByMemberId(mid);
        }
        paraMap.put("mid",mid);
        paraMap.put("mt",mt);
        paraMap.put("member",member);
        paraMap.put("account",account);
        return new ModelAndView("/wx/mt/gopay", paraMap);
    }
}
